<script setup lang="ts">
// const onClickLeft = () => {
//   console.log('onClickLeft')
// }
// const onClickRight = () => {
//   console.log('onClickRight')
// }

import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { loginByPassword, getMobileCode, loginByCode } from '@/services/user'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast, type FormInstance } from 'vant'
import router from '@/router'
// 用户的数据仓库实例
const store = useUserStore()
// 手机号、密码、验证码
const mobile = ref('')
const password = ref('')
const code = ref('')
//是否同意用户协议
const agree = ref(false)
// 密码是否可见
const show = ref(false)
// 表单校验---登录功能-------
// 是否显示密码登录------
const isPass = ref(true)
const login = async () => {
  console.log('22222')
  if (!agree.value) return showToast('请勾选同意协议')
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByCode(mobile.value, code.value)
  console.log('请求的登录结果', res)
  store.setUser(res.data)
  // push 追加 在历史页面中加入
  // place 替换 把上一页覆盖
  router.replace('/user')
  console.log('发送请求---表单提交')
}

// ref实例
const FormRef = ref<FormInstance>()
// 获取验证码--------------
let timerId: number
const time = ref(0)
const send = async () => {
  if (time.value > 0) return
  // 手机号校验
  await FormRef.value?.validate('mobile')
  console.log('手机号码校验成功')

  // 发送请求
  getMobileCode(mobile.value, 'login')
  showToast('获取验证码成功')
  // 倒计时
  time.value = 20
  clearInterval(timerId)
  timerId = setInterval(() => {
    time.value--
    if (time.value <= 0) clearInterval(timerId)
  }, 1000)
}
</script>
<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div v-if="isPass" class="login-head" autocomplete="off">
      <h3>密码登录</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">短信验证码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <div v-else class="login-head" autocomplete="off">
      <h3>短信验证码登录</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">密码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form @submit="login" ref="FormRef" autocomplete="on">
      <van-field
        name="mobile"
        type="tel"
        v-model="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-if="isPass"
        :type="`${show ? 'text' : 'password'}`"
        v-model="password"
        placeholder="请输入密码"
        :rules="passwordRules"
      >
        <!-- 密码svg插槽 -->
        <template #button>
          <cp-icon
            @click="show = !show"
            :name="`login-eye-${show ? 'on' : 'off'}`"
          >
          </cp-icon>
        </template>
      </van-field>
      <van-field
        v-else
        placeholder="请输入验证码"
        :rules="codeRules"
        v-model="code"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round native-type="submit" type="primary"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login {
  &-page {
    padding-top: 20px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
// ::v-deep() {
// }
/* 怎么给全局的组件提供类型？
    写一个类型声明文件，declare module 'vue' 声明一个 vue 类型模块
    然后 interface GlobalComponents 书写全局组件的类型
    key组件名称支持大驼峰，value是组件类型,通过 typeof 组件实例得到
 */
</style>
